﻿using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Windows;
using Caliburn.Micro;
using JY.Common.Helpers;
using JY.Wpf.Core.Events;
using JY.Wpf.Core.Tools;
using SD.IOC.Core.Mediators;

namespace JY.Wpf.Core.ViewModelBases
{
    /// <summary>
    /// ViewModel基类
    /// </summary>
    [NotifyPropertyChanged]
    public class ViewModelBase : Conductor<object>, IHandle<FlipperEvent>
    {
        #region # 变量

        #endregion

        #region 构造器

        public ViewModelBase()
        {
        }
        #endregion

        #region # 属性

        #region 唯一标识——Guid Id

        /// <summary>
        /// 唯一标识
        /// </summary>
        public readonly Guid Id = Guid.NewGuid();
        #endregion

        #region 事件——IEventAggregator EventAggregator
        /// <summary>
        /// 事件
        /// </summary>
        public IEventAggregator EventAggregator
        {
            get
            {
                return ResolveMediator.Resolve<IEventAggregator>();
            }
        }
        #endregion

        #region 标题——string Title
        /// <summary>
        /// 标题
        /// </summary>
        [NotifyPropertyChanged]
        public string Title { get; set; }
        #endregion

        #region 遮罩相关

        #region 是否显示遮罩——bool IsBusy

        /// <summary>
        /// 是否显示遮罩
        /// </summary>
        [NotifyPropertyChanged]
        public bool IsBusy { get; set; }

        #endregion

        #region 显示遮罩的内容——object BusyContent
        /// <summary>
        /// 显示遮罩的内容
        /// </summary>
        [NotifyPropertyChanged]
        public object BusyContent { get; set; }
        #endregion

        #endregion

        #region 是否切换页面——bool IsFlipped
        /// <summary>
        /// 是否切换页面
        /// </summary>
        [NotifyPropertyChanged]
        public bool IsFlipped { get; set; }
        #endregion
        #endregion

        #region # Event方法

        #region # 翻转内容——void Handle(FlipperEvent eventData)
        /// <summary>
        /// 翻转内容
        /// </summary>
        public virtual void Handle(FlipperEvent eventData)
        {
            if (!EventAggregatorExtension.CheckCanPublishSubscriber(this, typeof(IHandle<FlipperEvent>)))
            {
                return;
            }
            this.IsFlipped = eventData.IsFlipped;
        }
        #endregion

        #endregion

        #region # 方法

        #region # 初始化页面——void InitView()
        /// <summary>
        /// 初始化页面
        /// </summary>
        public virtual void InitView()
        {

        }
        #endregion

        #region # 重置页面——void Reset()
        /// <summary>
        /// 重置页面
        /// </summary>
        public virtual void Reset()
        {

        }
        #endregion
        #endregion

        #region # 生命周期相关
        /// <summary>
        /// 页面激活
        /// </summary>
        protected override void OnActivate()
        {
            base.OnActivate();
        }

        /// <summary>
        /// 页面失活
        /// </summary>
        /// <param name="close"></param>
        protected override void OnDeactivate(bool close)
        {
            this.EventAggregator.UnSubscribe(this);
            //查找属性为ViewModelBase的进行处理
            Type type = this.GetType();
            var propertyInfos = type.GetProperties().Where(x => x.CustomAttributes.Any()).ToList();
            foreach (var propertyInfo in propertyInfos)
            {
                var viewModelPropertyAttribute = propertyInfo.GetCustomAttribute<ViewModelPropertyAttribute>();
                if (viewModelPropertyAttribute == null) continue;
                if (viewModelPropertyAttribute.IsList)
                {
                    var viewmodels=propertyInfo.GetValue(this) as IList;
                    if (viewmodels == null) continue;
                    foreach (var viewmodel in viewmodels)
                    {
                        if (viewmodel is ViewModelBase viewModelBase)
                        {
                            viewModelBase.Deactivate();
                        }
                    }
                }
                else
                {
                    if (propertyInfo.GetValue(this) is ViewModelBase viewModelBase)
                    {
                        viewModelBase.Deactivate();
                    }
                }
            }
            base.OnDeactivate(close);
            //释放内存
            FlushMemoryHelper.FlushMemory();
        }

        /// <summary>
        /// 页面失活
        /// </summary>
        public virtual void Deactivate()
        {
            this.OnDeactivate(true);
        }
        #endregion
    }
}
